package com.zhilianbao.leyaogo.ui.adapter.me;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.model.response.me.BalanceResponse;
import com.zhilianbao.leyaogo.view.popup.BalancePopup;

import java.util.List;

/**
 * Created by liu on 2016/12/2.
 */
public class BalanceAdapter extends SwipeMenuAdapter<BalanceAdapter.BalanceViewHolder> {
    private List<BalanceResponse> mList;
    private Context mContext;

    public BalanceAdapter(Context context, List<BalanceResponse> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_add_online, parent, false);
    }

    @Override
    public BalanceAdapter.BalanceViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new BalanceAdapter.BalanceViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(BalanceAdapter.BalanceViewHolder holder, int position) {
        holder.setData(mContext,position, mList.get(position));

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    public static class BalanceViewHolder extends RecyclerView.ViewHolder {

        public CheckBox mBalanceCb;
        private TextView mStoreNum;
        private Context mContext;
        private RelativeLayout mRlBalanceItem;
        private TextView mTvGoodsGivenOne;
        private TextView mTvGoodsGivenTwo;
        private ImageView mIvHasMore;

        public BalanceViewHolder(View itemView) {
            super(itemView);
            mBalanceCb = (CheckBox) itemView.findViewById(R.id.balance_rb);
            mStoreNum = (TextView) itemView.findViewById(R.id.store_num);
            mRlBalanceItem = (RelativeLayout) itemView.findViewById(R.id.rl_balance_item);
            mTvGoodsGivenOne = (TextView) itemView.findViewById(R.id.tv_given_goods_one);
            mTvGoodsGivenTwo = (TextView) itemView.findViewById(R.id.tv_given_goods_two);
            mIvHasMore = (ImageView) itemView.findViewById(R.id.iv_has_more);

        }

        public void setData(Context context, int position, BalanceResponse balanceResponse) {
            mContext = context;
            //设置CheckBox的选中状态
            mBalanceCb.setClickable(false);
            if (balanceResponse.getIsCheckPosition() == 0 && position == 0) {
                balanceResponse.setIsCheck(true);
            }
            boolean isCheck = balanceResponse.getIsCheck();
            mBalanceCb.setChecked(isCheck);
            setViewColorOrVisible(context, isCheck);

            List<BalanceResponse.RechargeGoodsItemListBean> rechargeGoodsItemList = balanceResponse.getRechargeGoodsItemList();
            if (rechargeGoodsItemList == null || rechargeGoodsItemList.size() == 0) {
                setGivenGoodsVisbility(View.GONE, View.GONE, View.GONE);
            } else if (rechargeGoodsItemList.size() == 1) {
                setGivenGoodsVisbility(View.VISIBLE, View.GONE, View.GONE);
                setCouponOrGood(rechargeGoodsItemList);
            } else if (rechargeGoodsItemList.size() == 2) {
                setGivenGoodsVisbility(View.VISIBLE, View.VISIBLE, View.GONE);
                setCouponOrGood(rechargeGoodsItemList);
                setCouponOrGoods(rechargeGoodsItemList);
            } else {//显示箭头
                setGivenGoodsVisbility(View.VISIBLE, View.VISIBLE, View.VISIBLE);
                setCouponOrGood(rechargeGoodsItemList);
                setCouponOrGoods(rechargeGoodsItemList);
            }
            double topUpAmount = balanceResponse.getTopUpAmount();
            mStoreNum.setText(topUpAmount + "元");
            mIvHasMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BalancePopup balancePopup = new BalancePopup((Activity) mContext);
                    balancePopup.setBalanceAdapter(new BalanceListAdapter(mContext, rechargeGoodsItemList));
                    balancePopup.show(mIvHasMore);
                }
            });

        }

        //判断赠送优惠券还是商品,mTvGoodsGivenOne的设置
        private void setCouponOrGood(List<BalanceResponse.RechargeGoodsItemListBean> rechargeGoodsItemList) {
            if (rechargeGoodsItemList.get(0).getItemType() == 0) {//送优惠券
                mTvGoodsGivenOne.setText(rechargeGoodsItemList.get(0).getCouponsName());
            } else if (rechargeGoodsItemList.get(0).getItemType() == 1) {
                mTvGoodsGivenOne.setText(rechargeGoodsItemList.get(0).getGoodsName());
            }
        }

        //判断赠送优惠券还是商品,mTvGoodsGivenTwo的设置
        private void setCouponOrGoods(List<BalanceResponse.RechargeGoodsItemListBean> rechargeGoodsItemList) {
            if (rechargeGoodsItemList.get(1).getItemType() == 0) {//送优惠券
                mTvGoodsGivenTwo.setText(rechargeGoodsItemList.get(1).getCouponsName());
            } else if (rechargeGoodsItemList.get(1).getItemType() == 1) {
                mTvGoodsGivenTwo.setText(rechargeGoodsItemList.get(1).getGoodsName());
            }
        }

        //条目选中或未选中条目颜色背景的设置
        private void setViewColorOrVisible(Context context, boolean isCheck) {
            //根据选中状态设置条目
            if (isCheck) {
                mStoreNum.setTextColor(context.getResources().getColor(R.color.color_f91c4c));
                setSelectView(context.getResources().getColor(R.color.color_f91c4c), R.drawable.corner_rec_red_devider, R.drawable.ic_select_y);
                mIvHasMore.setImageResource(R.drawable.city_content_list_forward_default);
            } else {
                mIvHasMore.setImageResource(R.drawable.icon_next);
                mStoreNum.setTextColor(context.getResources().getColor(R.color.color_333333));
                setSelectView(context.getResources().getColor(R.color.color_999999), R.drawable.corner_rec_gray_devider, 0);
            }
        }

        //设置选中状态的文字颜色,背景
        private void setSelectView(int color, int background, int cbBackground) {
            mTvGoodsGivenOne.setTextColor(color);
            mTvGoodsGivenTwo.setTextColor(color);
            mRlBalanceItem.setBackgroundResource(background);
            mBalanceCb.setBackgroundResource(cbBackground);
        }

        //设置赠送商品的显示与否
        private void setGivenGoodsVisbility(int a, int b, int c) {
            mTvGoodsGivenOne.setVisibility(a);
            mTvGoodsGivenTwo.setVisibility(b);
            mIvHasMore.setVisibility(c);
        }

    }
}